<?php
namespace admin;
use models\BaseDoo;

class Supplier extends Admin{
    protected $dataBase;
    public function __construct()
    {
        $this->assign('lh_php','LH_PHP');
        $this->dataBase = new BaseDoo();
        parent::__construct();
    }

    //管理员管理
    public function index(){
        $this->assign('title','管理员列表');
        $field = ['id','phone','company_name','username','logo','create_time','account_number','supplier_name','address'];
        $supplier_list = $this->dataBase->select('supplier',$field,['ORDER'=>['id'=>'ASC'],'LIMIT'=>50]);
        $this->assign('supplier_list',$supplier_list);
        $this->display('supplier/index');
    }

    //添加管理员
    public function add(){
        if(isset($_POST['supplier_submit'])){
            unset($_POST['supplier_submit']);
            $_POST['password'] = sha1($_POST['password']);
            $this->dataBase->insert('supplier',$_POST);
            if($this->dataBase->id()){
                $this->success('/admin/supplier','添加成功');
            }else{
                $this->error('/admin/supplier','添加失败');
            }
        }
        $this->display('supplier/add');
    }

    //管理员修改
    public function edit(int $id){
        $find = $this->dataBase->get('supplier','*',['id'=>$id]);
        $this->assign($find);
        $this->display('supplier/edit');
    }

    //正式进行修改
    public function update(){
        $id = $_POST['id'];
        unset($_POST['id']);
        if(!empty($_POST['password'])){
            $_POST['password'] = sha1($_POST['password']);
        }else{
            //如果密码为空的话，就不参与修改了
            unset($_POST['password']);
        }
        $update = $this->dataBase->update('supplier',$_POST,['id'=>$id]);
        if($update){
            $this->success('/admin/supplier','修改成功');
        }else{
            $this->error('/admin/supplier','修改失败');
        }
    }


    //进行删除操作
    public function delete($id){
        if($this->dataBase->delete('supplier',['id'=>$id])){
            return show(1,'删除成功',[]);
        }else{
            return show(0,'删除失败',[]);
        }
    }


}